package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.domain.dto.CldInsuranceDetailsDto;
import com.cleaningcloud.cld.domain.vo.CldInsuranceDetailsVo;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldInsuranceDetailsMapper;
import com.cleaningcloud.cld.domain.CldInsuranceDetails;
import com.cleaningcloud.cld.service.ICldInsuranceDetailsService;

/**
 * 保险理赔明细Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-23
 */
@Service
public class CldInsuranceDetailsServiceImpl implements ICldInsuranceDetailsService 
{
    @Autowired
    private CldInsuranceDetailsMapper cldInsuranceDetailsMapper;

    /**
     * 查询保险理赔明细
     * 
     * @param insuranceDetailsId 保险理赔明细主键
     * @return 保险理赔明细
     */
    @Override
    public CldInsuranceDetails selectCldInsuranceDetailsByInsuranceDetailsId(Long insuranceDetailsId)
    {
        return cldInsuranceDetailsMapper.selectCldInsuranceDetailsByInsuranceDetailsId(insuranceDetailsId);
    }

    /**
     * 查询保险理赔明细列表
     * 
     * @param cldInsuranceDetails 保险理赔明细
     * @return 保险理赔明细
     */
    @Override
    public List<CldInsuranceDetailsVo> selectCldInsuranceDetailsList(CldInsuranceDetailsDto cldInsuranceDetails)
    {
        return cldInsuranceDetailsMapper.selectCldInsuranceDetailsList(cldInsuranceDetails);
    }

    /**
     * 新增保险理赔明细
     * 
     * @param cldInsuranceDetails 保险理赔明细
     * @return 结果
     */
    @Override
    public int insertCldInsuranceDetails(CldInsuranceDetails cldInsuranceDetails)
    {
        cldInsuranceDetails.setStaffInsuranceStatus(1L);
        cldInsuranceDetails.setFlag(0L);
        cldInsuranceDetails.setCreateTime(DateUtils.getNowDate());
        return cldInsuranceDetailsMapper.insertCldInsuranceDetails(cldInsuranceDetails);
    }

    /**
     * 修改保险理赔明细
     * 
     * @param cldInsuranceDetails 保险理赔明细
     * @return 结果
     */
    @Override
    public int updateCldInsuranceDetails(CldInsuranceDetails cldInsuranceDetails)
    {
        CldInsuranceDetails insuranceDetails = cldInsuranceDetailsMapper.selectCldInsuranceDetailsByInsuranceDetailsId(cldInsuranceDetails.getInsuranceDetailsId());
        // 出险
        if(cldInsuranceDetails.getStaffInsuranceStatus() == 2){
            cldInsuranceDetails.setDangerTime(DateUtils.getNowDate());
        }
        // 结案
        if(cldInsuranceDetails.getStaffInsuranceStatus() == 3){
            if(insuranceDetails.getInvoiceAmount() == null || insuranceDetails.getClaimsMoney() == null || insuranceDetails.getSubsidesMoney() == null){
                throw new RuntimeException("需填写发票金额、理赔金额、补助金额才可进行结案操作！");
            }
            cldInsuranceDetails.setCaseTime(DateUtils.getNowDate());
        }
        // 撤案
        if(cldInsuranceDetails.getStaffInsuranceStatus() == 4){
            cldInsuranceDetails.setDismissTime(DateUtils.getNowDate());
            cldInsuranceDetails.setDismissPersonName(SecurityUtils.getUsername());
        }
        return cldInsuranceDetailsMapper.updateCldInsuranceDetails(cldInsuranceDetails);
    }

    /**
     * 批量删除保险理赔明细
     * 
     * @param insuranceDetailsIds 需要删除的保险理赔明细主键
     * @return 结果
     */
    @Override
    public int deleteCldInsuranceDetailsByInsuranceDetailsIds(Long[] insuranceDetailsIds)
    {
        return cldInsuranceDetailsMapper.deleteCldInsuranceDetailsByInsuranceDetailsIds(insuranceDetailsIds);
    }

    /**
     * 删除保险理赔明细信息
     * 
     * @param insuranceDetailsId 保险理赔明细主键
     * @return 结果
     */
    @Override
    public int deleteCldInsuranceDetailsByInsuranceDetailsId(Long insuranceDetailsId)
    {
        return cldInsuranceDetailsMapper.deleteCldInsuranceDetailsByInsuranceDetailsId(insuranceDetailsId);
    }
}
